package com.eascs.workflow.timeEffective.dao.impl;

import java.util.List;
import org.springframework.stereotype.Repository;
import com.eascs.workflow.common.dao.BaseDao;
import com.eascs.workflow.common.entity.wf.ProcessTaskMain;
import com.eascs.workflow.common.entity.wf.ProcessTaskNode;
import com.eascs.workflow.timeEffective.dao.TimeffectiveDao;

@Repository
public class TimeffectiveDaoImpl extends BaseDao implements TimeffectiveDao{

	@Override
	public List<ProcessTaskNode> queryProcessNodeByPage(int pageSize,int pageIdex) {
		if (pageSize  == 0) {
			return null;
		}
		StringBuffer sb=new StringBuffer();
		sb.append(" SELECT n.* FROM wf_process_task_main m,wf_process_task_node n");
		sb.append(" WHERE n.task_id=m.task_id");
		sb.append(" AND m.time_effective IS NOT NULL");
		sb.append(" AND n.node_status in (1,3,5)");
		sb.append(" AND m.process_status in (0,2,3)");
		//sb.append(" AND n.used_time IS NULL");
		sb.append(" LIMIT ?,?");
		int start=pageSize*pageIdex;
		return super.findEntitys(sb.toString(), ProcessTaskNode.class, new Object[]{start,pageSize});
	}

	@Override
	public int queryTotalCount() {
		StringBuffer sb=new StringBuffer();
		sb.append(" SELECT count(*) FROM wf_process_task_node m,wf_process_task_node n");
		sb.append(" WHERE n.task_id=m.task_id");
		sb.append(" AND m.time_effective IS NOT NULL");
		sb.append(" AND n.node_status in (3,5)");
		sb.append(" AND n.used_time IS NULL");
		return super.findInteger(sb.toString(), new Object[]{});
	}

	@Override
	public boolean updateUsedTime(String taskNodeId, float usedTime) {
		String sql = "update wf_process_task_node set used_time=? where task_node_id=?";
		int result = super.execute(sql, new Object[]{usedTime,taskNodeId});
		
		return result > 0 ? true : false;
	}

	@Override
	public boolean updateWFMainUsedTime(String taskMainId, float usedTime, float overTime) {
		String sql = "UPDATE wf_process_task_main SET used_time=?,overtime=? WHERE task_id=?";
		int result = super.execute(sql, new Object[]{usedTime,overTime,taskMainId});
		
		return result > 0 ? true : false;
	}

	@Override
	public List<ProcessTaskMain> queryProcessMainByPage(int pageSize, int pageIdex) {
		if (pageSize  == 0) {
			return null;
		}
		String sql ="SELECT * FROM wf_process_task_main LIMIT ?,?";
		int start=pageSize*pageIdex;
		return super.findEntitys(sql, ProcessTaskMain.class, new Object[]{start,pageSize});
	}



}
